var form = layui.form,id=getQueryVariable("id");



var productCode=parent.code;
var zhuge=parent.fjCode;


/*$("input[name='productCode']").attr("readOnly",true);
$("input[name='fjCode']").attr("readOnly",true);*/
//表单初始值，修改时调用
if(id!=""){
   /* form.val("editor-form",{
        "productcode":"进入修改",productsatableid
    })*/
/*, {field: 'productCode', title: '产品编码', sort: true}
, {field: 'fjCode', title: '规格编码', sort: true}
, {field: 'cpbl', title: '成品比例', sort: true}
, {field: 'pjbl', title: '配件比例', sort: true}
, {field: 'chinaname', title: '中文名称', sort: true}
, {field: 'englishname', title: '英文名称', sort: true}
, {field: 'mz', title: '毛重', sort: true}
, {field: 'jz', title: '净重', sort: true}
, {field: 'tj', title: '体积', sort: true}*/
    var oldData =  parent.table.cache["productsatableid"];
    $.each(oldData, function (index, value) {
        if (oldData[index]["id"] == id) {
            form.val("editor-form",{
                "productCode":oldData[index]["productCode"],
                "fjCode":oldData[index]["fjCode"],
                "cpbl":oldData[index]["cpbl"],
                "pjbl":oldData[index]["pjbl"],
                "chinaname":oldData[index]["chinaname"],
                "englishname":oldData[index]["englishname"],
                "mz":oldData[index]["mz"],
                "jz":oldData[index]["jz"],
                "tj":oldData[index]["tj"]

            })
        }
    });
    form.render();
}else{

        form.val("editor-form",{
            "productCode":"",//productCode,
            "fjCode":"",//fjCode,
            "cpbl":"",
            "pjbl":"",
            "chinaname":"",
            "englishname":"",
            "mz":"",
            "jz":"",
            "tj":""

        })
    form.render();
}
//监听提交
form.on('submit(form-element)', function(data){
    //data.field 为表单修改或新增数据 变量ID有值的情况下是修改，否则为新增
    data.field.zhuge=zhuge;
    data.field.mainProductCode=productCode;
    if(savege(data.field)){
        layer.msg("保存成功", {icon: 6});
        parent.refrash_gexh();
        //保存成功关闭此页面
        var index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(index);
    }else{
        layer.msg("保存失败", {icon: 6});
    }

    /*setTimeout(function () {

    },1000)*/
    return false;
});
function  savege(data) {
    if(id !="") data.id=id;


    var tf=false;
    $.ajax({
        type : 'post',
        //  url : 'resource/getTree',
        data:JSON.stringify(data),
        url:'productfjge/savege',
        dataType:"json",
        contentType: 'application/json',
        async:false,
        success : function (da) {
            if(da.status==200 ){
                tf= da.data >0 ;
               // id=da.data;//
            }
        },
        error: function(data) {
            // layer.msg("获取失败!");
        }
    });
    return tf;
}